{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1ede8c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import h5py\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "import os\n",
    "import os.path as osp\n",
    "import imageio.v2 as imageio\n",
    "\n",
    "from scipy.stats import describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2112c72a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import cv2\n",
    "def imread_cv2(path, options=cv2.IMREAD_COLOR):\n",
    "    \"\"\"Open an image or a depthmap with opencv-python.\"\"\"\n",
    "    if path.endswith((\".exr\", \"EXR\")):\n",
    "        options = cv2.IMREAD_ANYDEPTH\n",
    "    img = cv2.imread(path, options)\n",
    "    if img is None:\n",
    "        raise IOError(f\"Could not load image={path} with {options=}\")\n",
    "    if img.ndim == 3:\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "    return img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "709073f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "s = 0\n",
    "# List of file paths\n",
    "root = \"/lc/data/3D/7scenes/office/seq-03\"\n",
    "file_paths = sorted([f for f in os.listdir(root) if f.endswith(\"color.png\")])\n",
    "\n",
    "cols = 2\n",
    "rows = 10\n",
    "interval = 1\n",
    "# Create a figure with 2 columns and 10 rows,dpi=100\n",
    "fig, axs = plt.subplots(rows, cols, figsize=(12, rows*3))\n",
    "\n",
    "# Iterate over the file paths and display the images\n",
    "for i, file_path in enumerate(file_paths[s:rows*cols*interval//2+s:interval]):\n",
    "    img = mpimg.imread(osp.join(root, file_path))\n",
    "    depth = imread_cv2(osp.join(root, file_path.replace(\"color.png\",\"depth.png\")), cv2.IMREAD_UNCHANGED)/1000\n",
    "    cam_f = osp.join(root, file_path.replace(\"color.png\",\"pose.txt\"))\n",
    "    pose_matrix = np.loadtxt(cam_f, delimiter=None)\n",
    "    cam_desc = f'{pose_matrix[:3, -1]}'\n",
    "    desc = f'{depth.min()}, {depth.max()}, {depth.mean()}'\n",
    "    depth[depth == 1.0] = 0\n",
    "    axs[i * 2 // cols , i * 2 % cols ].imshow(img)\n",
    "    axs[i * 2 // cols , i * 2 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    fname = file_path.split('.')[0].split('-')[1]\n",
    "    axs[i * 2 // cols , i * 2 % cols].set_title(cam_desc)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].imshow(depth)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].set_title(desc)\n",
    "\n",
    "plt.show()\n",
    "# continious pose with slow motion\n",
    "# poses correct, but depth noisy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2616181c",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = mpimg.imread(osp.join(root, file_path))\n",
    "print(img.shape)\n",
    "cam_f = osp.join(root, file_path.replace(\"color.png\",\"pose.txt\"))\n",
    "pose_matrix = np.loadtxt(cam_f, delimiter=None)\n",
    "print(pose_matrix.shape)\n",
    "print(pose_matrix)\n",
    "# (480, 640, 3)\n",
    "# (4, 4) no intrinsics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45a1b34d",
   "metadata": {},
   "outputs": [],
   "source": [
    "depth = mpimg.imread(osp.join(root, file_path.replace(\"color.png\",\"depth.png\")))\n",
    "print(depth.shape, depth.dtype) \n",
    "print(describe(depth, axis=None))\n",
    "print(describe(depth*65535, axis=None))\n",
    "# import cv2\n",
    "# cv_depth = cv2.imread(osp.join(root, file_path.replace(\"color.png\",\"depth.png\")), cv2.IMREAD_ANYDEPTH)\n",
    "# print(describe(cv_depth, axis=None))\n",
    "# print(describe(cv_depth/65535, axis=None))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vggt",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
